<template>
	<view class="contain">
		
		<view class="services">
			<view @click="phonecall"><u-icon name="kefu-ermai" color="#6B6B6B" size="28"></u-icon></view>
			<view @click="reload"><u-icon name="reload" color="#6B6B6B" size="28"></u-icon></view>
			<!-- <view><u-icon name="bell" color="#6B6B6B" size="28"></u-icon></view> -->
			<!-- <view><u-icon name="setting" color="#6B6B6B" size="28"></u-icon></view> -->
		</view>
		
		<view class="head">
			<view class="userinfo">
				<view class="himg"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/bg/head.png"></image></view>
				<view class="hinfo" v-if="userid!=null && userid!=''">
					<text class="blod phonetext">{{hiddenPhone(phone)}}</text>
					<text class="small black">ID: {{userid}}</text>
				</view>
				<view class="hinfo" v-else>
					<u-button type='primary' text="尚未登录" @click="goLogin"></u-button>
				</view>
			</view>
			<view class="sign">
				
			</view>
		</view>

		<view class="options">
			<view><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/bg/sd.png" @click="jUrl('',2)"></image></view>
			<view><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/bg/cp.png" @click="alertmsg"></image></view>
		</view>
		
		 <view class="GridBox">
			<!-- <view class="gtitle"><text class="mid black">学习</text></view> -->
			 <view class="Night">
					 <view class="gridbg border-r-[1px] border-gray-100" @click="GoBuyCeping">
						<view class="iconsbg"><image style="width:150rpx !important;height:150rpx !important;" src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/wxpay.png" mode="aspectFit"></image></view>
						<view class="wordText"><text>测评购买</text></view>
					 </view>
					 <view class="gridbg border-r-[1px] border-gray-100" @click="jUrl('/pages/buy/buy')">
						<view class="iconsbg"><image style="width:150rpx !important;height:150rpx !important;" src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/wxpay.png" mode="aspectFit"></image></view>
						<view class="wordText"><text>购买课程</text></view>
					 </view>
					 <view class="gridbg border-r-[1px] border-gray-100" @click="jUrl('/pages/sjorders/sjorders')">
						<view class="iconsbg"><image style="width:150rpx !important;height:150rpx !important;" src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/yuyue.png" mode="aspectFit"></image></view>
						<view class="wordText"><text>我的订单</text></view>
					 </view>
					 <view class="gridbg" @click="jUrl('/pages/binduser/binduser')">
					 	<view class="iconsbg"><u-icon name="lock-open" color="#2979ff" size="32"></u-icon></view>
					 	<view class="wordText"><text>绑定筛查</text></view>
					 </view>
					<!-- <view class="gridbg border-r-[1px] border-gray-100" @click="jUrl('/pages/myfav/myfav')">
						<view class="iconsbg"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/guanzhu.png" mode="aspectFit"></image></view>
						<view class="wordText"><text>我的收藏</text></view>
					</view> -->
					 <view class="gridbg" @click="jUrl('/pages/zhidao/zhidao')">
					 	<view class="iconsbg"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/yaoqiu.png" mode="aspectFit"></image></view>
					 	<view class="wordText"><text>操作指导</text></view>
					 </view>
					 <view class="gridbg" @click="golink">
					 	<view class="iconsbg"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/study.png" mode="aspectFit"></image></view>
					 	<view class="wordText"><text>疏导学习</text></view>
					 </view>
					 <view class="gridbg" @click="jUrl('/pages/studyprogress/studyprogress')">
					 	<view class="iconsbg"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/progress.png" mode="aspectFit"></image></view>
					 	<view class="wordText"><text>课程进度</text></view>
					 </view>
					 <view class="gridbg" @click="jUrl('/pages/yuyue/yuyue')">
					 	<view class="iconsbg"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/yytime.png" mode="aspectFit"></image></view>
					 	<view class="wordText"><text>我的预约</text></view>
					 </view>
					 <!-- v-if="usertpye==='特殊'" -->
					 <view class="gridbg" @click="gotongji" v-if="usertpye==='特殊'">
					 	<view class="iconsbg"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/icons/tongji.png" mode="aspectFit"></image></view>
					 	<view class="wordText"><text>数据统计</text></view>
					 </view>
					<view class="gridbg" @click="loginOut">
						<view class="iconsbg"><u-icon size="30" name="share-square"></u-icon></view>
						<view class="wordText"><text>退出登录</text></view>
					</view>
			 </view>
		 </view> <!-- GridBox End -->
	</view>
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import {  onLoad,onShow } from '@dcloudio/uni-app'
import { userStore } from '@/stores/userStore'
import {hiddenPhone} from '@/common/common.js'
let store = userStore();	
let { hasLogin,userData } = userStore();


let userid=ref('');
let usertpye=ref('');
let phone=ref('');

const goLogin=()=>{
	let keyid=uni.getStorageSync("keyid") || null;
	let url="/pages/login/login";
	if(keyid!=null){
		url="/pages/login/login?keyid="+keyid;
	}
	console.log("跳转登录链接",url);
	//uni.$tools.Toast("准备登录");
	uni.$tools.GoUrl(url,1);
}


//检查测评是否已购买，未购买则跳转过去购买
const GoBuyCeping=()=>{
	let paraData={
		id:userData.userinfo.id,
	}
	uni.$tools.Reqeust('/api/CheckFuShuCePingUserBuy',paraData,'POST').then((res:any)=>{
		console.log("检查测评是否已购买",res);
		if(res.statusCode===200){
			if(res.data.isBuy===true){
				jUrl('/pages/savefushuewm/savefushuewm') //已购买，直接跳转到二维码下载页面
			}else{
				jUrl('/pages/cepingbuy/cepingbuy')
			}
		}else{
			if(res.data.msg!=null){
				uni.$tools.Toast(res.data.msg);
			}
		}
	});
	
}


const phonecall=()=>{
	uni.makePhoneCall({
		phoneNumber: '18420470708' //仅为示例
	});
}

const gotongji=()=>{
	uni.$tools.GoUrl("/pages/tongji/tongji");		
}


//重新刷新用户信息
const reload=()=>{
	uni.showLoading({
		title:"正在刷新.."
	})
	setTimeout(()=>{
		uni.hideLoading();
	},1000);
	let paraData={
		id:userData.userinfo.id,
	}
	console.log("重新刷新用户信息参数:",paraData);
	uni.$tools.Reqeust('/api/getUserInfo',paraData,'POST').then((res:any)=>{
		console.log(res);
		if(res.data.code===200){
			userData.userinfo=res.data.newuserinfo;
			phone.value=res.data.newuserinfo.phone;
			userid.value=res.data.newuserinfo.id;
			usertpye.value=res.data.newuserinfo.utype;
			uni.$tools.Toast("已刷新");
		}
	});
}

const golink=()=>{
	if(userData.userinfo===null || hasLogin===false){
		uni.$tools.Toast("会话失效请重新登录");
		setTimeout(()=>{
			uni.$tools.GoUrl('/pages/login/login',0);
		},1000);
		return;
	}
	// if(userData.userinfo.h5link!=null && userData.userinfo.h5link!=''){
	// 	let h5link=userData.userinfo.h5link;
	// 	let accessUrl="/pages/webview/webview";
	// 	store.backRouter=h5link;
	// 	uni.$tools.GoUrl(accessUrl);
	// }else{
	// 	uni.$tools.Toast("暂时无法进行疏导学习");
	// }
	uni.showLoading({
		title:"正在获取连接"
	})
	setTimeout(()=>{
		uni.hideLoading();
	},3000);
	let paraData={
		id:userData.userinfo.id,
		token:uni.getStorageSync("token")?uni.getStorageSync("token"):null
	}
	console.log("登录参数:",paraData);
	uni.$tools.Reqeust('/api/getH5Link',paraData,'POST').then((res:any)=>{
		console.log("获取H5链接",res);
		if(res.data.code===200){
			uni.hideLoading();
			if(res.data.h5Link!=null && res.data.h5Link!=""){
				let h5link=res.data.h5Link;
				let accessUrl="/pages/webview/webview";
				store.backRouter=h5link;
				uni.$tools.GoUrl(accessUrl);			
			}else{
				uni.$tools.Toast("系统维护中!暂时无法学习");
			}
		}else{
			uni.$tools.Toast(res.data.msg);
		}
	});
}

const jUrl=(url:string,num?:number)=>{
	if(userData.userinfo===null || hasLogin===false){
		uni.$tools.Toast("会话失效请重新登录");
		setTimeout(()=>{
			uni.$tools.GoUrl('/pages/login/login',0);
		},1000);
		return;
	}
	if(num!=undefined){
		uni.$tools.GoUrl(url,num);
	}else{
		uni.$tools.GoUrl(url);
	}
}		

const updateLoginTime=()=>{
	let paraData={
		id:userData.userinfo.id
	}
	console.log("更新时间参数:",paraData);
	uni.$tools.Reqeust('/api/updateLoginTime',paraData,'POST').then((res:any)=>{
		console.log(res);
	});	
}



onShow(()=>{
	if(userData.userinfo!=null){
		console.log("更新用户信息")
		phone.value=userData.userinfo.phone;
		userid.value=userData.userinfo.id;
		usertpye.value=userData.userinfo.utype;
		updateLoginTime();
	}
});

const loginOut=()=>{
	uni.showModal({
		title:"系统提示",
		content:"是否退出登录?",
		success:res=>{
			if(res.confirm){
				uni.clearStorageSync();
				uni.clearStorage();
				uni.$tools.GoUrl('/pages/login/login',1);				
			}
		}
	})
}

const alertmsg=()=>{
	uni.$tools.Toast("功能即将开放");
}

</script>
<style>
	page{
		padding-bottom:200rpx;
		background: #daeef7; /*//58d0f5 daeef7*/
	}
</style>
<style lang='scss' scoped>
	@import "mine.scss";
</style>